import os

import cv2
import numpy as np
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator


# 微调模型
from matplotlib import pyplot as plt

test_dir = "G:\\python\\keshe\\test1\\"
# model=load_model('./data/cats_and_dogs2.h52')    # 加载模型参数
model=load_model('C:\\Users\\23893\\PycharmProjects\\pythonProject1\\keshe\\data\\cats_and_dogs_wei_tiao.h5')    # 加载模型参数


plt.figure(figsize=(150, 150))
for i in range(30):
    plt.subplot(5, 6, i + 1)
    j=i+1
    print(str(i) + '.jpg')
    img=cv2.imread(test_dir+str(j)+'.jpg')
    b, g, r = cv2.split(img)
    img = cv2.merge((r, g, b))

    print(img.shape)

    img=cv2.resize(img,(150,150))
    print(img.shape)        # 读取图片并将大小统一至模型输入要求



    img0 = (img.reshape(1,150,150,3).astype("float32"))/255    # 归一化
    predict = model.predict(img0)
    predict = np.int64(predict > 0.5)


    if predict == 0:
        print('识别为：猫')
        image1=cv2.putText(img,'cat',(20,20),cv2.FONT_HERSHEY_SIMPLEX,0.5,(11,131,212),1,cv2.LINE_AA)

        plt.imshow(image1)
        plt.xticks([])
        plt.yticks([])

    else:
        image2=cv2.putText(img, 'dog', (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255,0), 1, cv2.LINE_AA)
        print('识别为：狗')

        plt.imshow(image2)
        plt.xticks([])
        plt.yticks([])

plt.show()




